	<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<title>后台管理</title>
<link rel="icon"
	href="${pageContext.request.contextPath}/lib/static/login/images/favicon.ico">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/lib/layui/css/layui.css">
<script
	src="${pageContext.request.contextPath}/lib/static/common/js/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/lib/layui/layui.js"
	charset="utf-8"></script>
<script
	src="${pageContext.request.contextPath}/lib/static/common/js/jquery.form.js"></script>
</head>
<body>
	<div class="layui-fluid">
		<div class="layui-row">
			<fieldset class="layui-elem-field layui-field-title">
				<legend>菜单管理</legend>
			</fieldset>
		</div>
		<hr class="layui-bg-red">
	<div>
		<button class="layui-btn layui-btn-sm"
			onclick="window.location.href='${pageContext.request.contextPath}/sys/menu/menu_ui';">刷新</button>
		<button class="layui-btn layui-btn-sm" onclick="add(null);">新增</button>
		<button class="layui-btn layui-btn-sm" onclick="deletes(null);">多项删除</button>
	</div>
	<div>
		<table class="layui-hidden" id="listTable" lay-filter="listTable"></table>
	</div>
	
	<script>
        var editObj=null,ptable=null,dltable=null,tableId='listTable',layer=null;
        layui.config({
        	base: '${pageContext.request.contextPath}/lib/layui/extend/'
		}).extend({
        	  dltable:'dltable' }).use(['jquery','dltable','layer']
        		, function(){
        			var $=layui.jquery;
        			treeGrid = layui.treeGrid;//很重要
        			layer=layui.layer;
       				ptable=treeGrid.render({
       					 id:tableId
        				,elem: '#'+tableId
       					,idField:'id'
        				,url:'${pageContext.request.contextPath}/sys/menu/getAll'
			            ,cellMinWidth: 100
			            ,treeId:'id'//树形id字段名称
			            ,treeUpId:'parentId'//树形父id字段名称
			            ,treeShowName:'name'//以树形式显示的字段
			            ,heightRemove:[".dHead",10]//不计算的高度,表格设定的是固定高度，此项不生效
			            ,height:'100%'
			            ,isFilter:false
			            ,iconOpen:true//是否显示图标【默认显示】
			            ,isOpenDefault:false//节点默认是展开还是折叠【默认展开】
			            ,loading:true
			            ,cols: [[
						        {type: 'checkbox', fixed: 'left'}
						        ,{field:'id', title:'菜单ID', width:80, fixed: 'left', unresize: true}
						        ,{field:'name', title:'菜单名称'}
						        ,{field:'typeName', title:'菜单类型'}
						        ,{width:170,title: '操作', align:'center'/*toolbar: '#barDemo'*/
						        ,templet: function(d){
		        					var html='';
							        var addByParentIdBtn='<a class="layui-btn layui-btn layui-btn-xs" lay-event="addByParentId">新增</a>';
							        var addBtn='<a class="layui-btn layui-btn layui-btn-xs" lay-event="update">编辑</a>';
							        var delBtn='<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
							        return addByParentIdBtn+addBtn+delBtn;
						          }
						         }
	        				   ]]
        				,isPage:false
					});

        treeGrid.on('tool('+tableId+')',function (obj) {
        	if(obj.event === 'del'){//删除行
        		del(obj);

        		}else if(obj.event==="update"){//添加行
        			update(obj);
        		   }else if(obj.event==="addByParentId"){//添加行
					 addByParentId(obj);
					}
        	});

        });

        /*新增*/
        function add() {
			layer.open({
				content: ['${pageContext.request.contextPath}/sys/menu/menu_edit'],
					type: 2,
						title: '<h2>添加菜单</h2>',
						maxmin: true,
						shadeClose: true,
						offset: '50px',
						area: ['800px', '520px'],
						closeBtn: 2,
						success: function (layero, index) {
							layer.iframeAuto(index);
						}
		    });
        }
		
		/*新增(上级菜单为选中行菜单)*/
		function addByParentId(obj) {
			layer.open({
				content: ['${pageContext.request.contextPath}/sys/menu/menu_addByParentId'],
				type: 2,
				title: '<h2>添加菜单</h2>',
				maxmin: true,
				shadeClose: true,
				offset: '50px',
				area: ['800px', '520px'],
				closeBtn: 2,
				success: function (layero, index) {
					var body = layer.getChildFrame('body', index);
					var data=obj.data;
					 //将选中的数据的id传到编辑页面的隐藏域，便于根据ID修改数据
					body.find("#parentId").val(data.id);
					layer.iframeAuto(index);
				}
			});
		}
        
           /* 编辑*/
        function update(obj){
		layer.open({
		content: ['${pageContext.request.contextPath}/sys/menu/menu_edit'],
		type: 2,
		title: '<h2>编辑菜单</h2>',
		maxmin: true,
		shadeClose: true,
		area: ['800px', '520px'],
		closeBtn: 2,
		success: function (layero, index) {
		var body = layer.getChildFrame('body', index);
		var data=obj.data;
		body.find("#name").val(data.name); //将选中的数据的id传到编辑页面的隐藏域，便于根据ID修改数据
		body.find("#parentId").val(data.parentId);
		body.find("#permission").val(data.permission);
		body.find("#url").val(data.url);
		body.find("#note").val(data.note);
		body.find("#id").val(data.id);
		body.find("#sort").val(data.sort);
		body.find("#type").val(data.type);
		layer.iframeAuto(index);
		},
		});
		}

             /*删除一个*/
        function del(obj) {
		   layer.confirm("确定是否删除所选项",
		   function(index){//确定回调
		     var id=obj.data.id;
             var url='${pageContext.request.contextPath}/sys/menu/deleteById';
        	 $.post(url,{id:id},function(result){
        		if(result.code==0){
		          layer.close(index);
				  window.location.href='${pageContext.request.contextPath}/sys/menu/menu_ui'
				  layer.msg("删除成功");
        		}
        		layer.msg(result.msg);
        		},"json");
        	},function (index) {//取消回调
        		layer.close(index);
        		}
        	);
        }

        /*删除多个*/
        function deletes() {
	        var checkStatus = treeGrid.checkStatus(tableId)
	        ,data = checkStatus.data;
	        if(!data[0]){
            	layer.msg("请先选择要删除的菜单");
            	return;
            	}
	        layer.confirm('确定是否删除所选项，如包含子菜单也将一并删除', function (index) {
	        var url = '${pageContext.request.contextPath}/sys/menu/deleteByIds';
	        var ids = [];
	        $.each( data, function(i, d){
        		var id = d.id;
        		ids.push(id);
        	});
        	var params = {"ids":ids.toString()};
            $.post(url,params,function(result){
	            if(result.code==0){
		            layer.close(index);
		            layer.msg("删除成功");
					window.location.href='${pageContext.request.contextPath}/sys/menu/menu_ui'
            	}
            	layer.msg(result.msg);
            },"json");
        	});
          }
        </script>
</div>
</body>
</html>